/**
 * 
 */
package org.unisannio.remote;

import it.unipr.ce.dsg.s2p.sip.Address;

import java.util.ArrayList;

import org.unisannio.util.ActivityForResultRequest;
import org.unisannio.util.ActivityRequest;
import org.unisannio.util.ActivityResult;

import android.content.Context;
import android.content.Intent;

/**
 * This class provide the mechanism for communicating towards other peers, for starting remote activities.
 * 
 * @author Fabio Melillo
 *
 */
public class RemoteActivity extends GenericRemote {

	public RemoteActivity(Context context) {
		super(context);
	}
	
	public RemoteActivity(Context context, int port){
		super(context, port);
	}
	
	/**
	 * start the activity specified in the intent, on the remote device with the reqCode set
	 * On the result the onActivityResult of the caller will be invoked
	 * 
	 * @param intent
	 * @param reqCode
	 */
	public void startActivityForResult(Intent intent, int reqCode){
		ActivityForResultRequest actForResReq=new ActivityForResultRequest(intent,reqCode);
		broadcastContentPeer.send(actForResReq);
	}
	
	/**
	 * start the activity specified in the intent, on the remote device
	 * 
	 * @param intent
	 */
	public void startActivity(Intent intent){
		intent.removeCategory(intent.getComponent().getClassName());
		intent.removeCategory("cmp");
		intent.setComponent(null);
		ActivityRequest actReq=new ActivityRequest(intent);
		broadcastContentPeer.send(actReq);
	}
	
	
	/**
	 * return the result to the remote caller
	 * 
	 * @param reqCode
	 * @param respCode - the code of the response, to forward to the caller
	 * @param intent - data elaborated
	 */
	public void returnActivityResult(int reqCode, int respCode, Intent intent){
		
		ActivityResult actRes=new ActivityResult(reqCode, respCode, intent);
		broadcastContentPeer.returnActivityResult(actRes);
	}
	
	/**
	 * Save the list of address chosen by the user
	 * 
	 * @param destinationList
	 */
	public void saveChosenAddress(ArrayList<Address> destinationList){
		broadcastContentPeer.setDestinationAddress(destinationList);
	}
	/**
	 * Check if there is any address already chosen
	 * 
	 * @return true if there is at least one address set
	 */
	public boolean isChosenAddress(){
		return broadcastContentPeer.isAddressSelected();
	}
	
	/**
	 * Gets the address of all the devices published into the SuperPeer
	 * which have shared the ContentProvider with the interestedUri 
	 * 
	 * @param interestedUri
	 * @return The list of Address of possible targets
	 */
	public ArrayList<Address> retrieveAvailableAddress(String interestedUri){
		return broadcastContentPeer.getTargetAddress(interestedUri);
	}

}
